Conditions | 4 |
Paths | 4 |
Total Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import { List } from 'immutable'; |
||
5 | export const moveTreeNode = ( |
||
6 | treeData, |
||
7 | currentIndex, |
||
8 | currentPath, |
||
9 | nextIndex, |
||
10 | nextPath, |
||
11 | childIdentifier = 'children', |
||
12 | rootIdentifier = 'root' |
||
13 | ) => { |
||
14 | |||
15 | const originalTreeData = treeData; |
||
16 | |||
17 | const { node: currentParent, indexPath: currentIndexPath } = findTreeNode( |
||
18 | treeData, |
||
19 | currentPath, |
||
20 | childIdentifier, |
||
21 | rootIdentifier |
||
22 | ); |
||
23 | |||
24 | if (!currentParent) { |
||
|
|||
25 | return originalTreeData; |
||
26 | } |
||
27 | |||
28 | currentIndexPath.push(...[childIdentifier, currentIndex]); |
||
29 | |||
30 | let node = treeData.getIn(currentIndexPath); |
||
31 | treeData = treeData.deleteIn(currentIndexPath); |
||
32 | |||
33 | const { node: nextParent, indexPath: nextIndexPath } = findTreeNode( |
||
34 | treeData, |
||
35 | nextPath, |
||
36 | childIdentifier, |
||
37 | rootIdentifier |
||
38 | ); |
||
39 | |||
40 | if (!nextParent) { |
||
41 | return originalTreeData; |
||
42 | } |
||
43 | |||
44 | nextIndexPath.push(childIdentifier); |
||
45 | |||
46 | if (!List.isList(treeData.getIn(nextIndexPath))) { |
||
47 | treeData = treeData.setIn(nextIndexPath, List()); |
||
48 | } |
||
49 | |||
50 | node = node.set('parentId', nextPath.last()); |
||
51 | |||
52 | treeData = treeData.setIn( |
||
53 | nextIndexPath, |
||
54 | treeData.getIn(nextIndexPath).insert(nextIndex, node) |
||
55 | ); |
||
56 | |||
57 | return treeData; |
||
58 | }; |
||
59 |